//
//  RootViewController.m
//  播放器HW
//
//  Created by dllo on 15/12/8.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RootViewController.h"
#import "MineViewController.h"
#import "RecommendViewController.h"
#import "STKAudioPlayer.h"
#import "PlayViewController.h"

@interface RootViewController ()

@property (nonatomic, retain) MineViewController *mineVC;
@property (nonatomic, retain) RecommendViewController *recommendVC;
@property (nonatomic, retain) STKAudioPlayer *player;
@property (nonatomic, retain) UIButton *playB;
@property (nonatomic, retain) UIButton *stopB;
@property (nonatomic, retain) UIButton *nextB;
@property (nonatomic, retain) UISlider *volS;
@property (nonatomic, retain) UISlider *timeS;
@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, retain) UITableView *FootV;
@property (nonatomic, retain) UILabel *song;
@property (nonatomic, retain) UILabel *singer;
@property (nonatomic, retain) UIImageView *songCover;

@end

@implementation RootViewController

- (void)dealloc
{
    [_mineVC release];
    [_recommendVC release];
    [_player release];
    [_playB release];
    [_stopB release];
    [_volS release];
    [_timeS release];
    [_timer release];
    [_nextB release];
    [_FootV release];
    [_song release];
    [_singer release];
    [_songCover release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.view.frame];
    imageV.image = [UIImage imageNamed:@"magic-radio@2x.png"];
    [self.view addSubview:imageV];
    [imageV release];
    
    self.mineVC = [[MineViewController alloc] init];
    [self addChildViewController:self.mineVC];
    [self.view addSubview:self.mineVC.view];
    [_mineVC release];
    
    self.recommendVC = [[RecommendViewController alloc] init];
    [self addChildViewController:self.recommendVC];
    [self.view addSubview:self.recommendVC.view];
    [_recommendVC release];
    
    self.FootV = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 49, self.view.frame.size.width, 49) style:UITableViewStylePlain];
    self.FootV.backgroundColor = [UIColor clearColor];
    [self.tabBarController.view addSubview:self.FootV];
    [_FootV release];
    
    
    UISegmentedControl *segmentC = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"我的", @"推荐", nil]];

    segmentC.frame = CGRectMake(50, 0, self.view.frame.size.width - 100, 44);
//    segmentC.backgroundColor = [UIColor blueColor];
    segmentC.selectedSegmentIndex = 1;
    [segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.navigationController.navigationBar addSubview:segmentC];
    [segmentC release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navigation_bar_btn_search@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(searchAction:)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"list_icon_music_man@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(personAction:)];
    
    
    STKAudioPlayerOptions playerOptions = {YES, YES, {50, 100, 200, 400, 800, 1600, 2600, 16000}};
    
    self.player = [[[STKAudioPlayer alloc] initWithOptions:playerOptions] autorelease];
    self.player.volume = 100;
    
    self.playB = [UIButton buttonWithType:UIButtonTypeCustom];
////    self.playB.frame = CGRectMake(self.view.frame.size.width - 120, self.view.frame.size.height - 45, 40, 40);
    self.playB.frame = CGRectMake(self.view.frame.size.width - 120, 5, 40, 40);
////    self.playB.backgroundColor = [UIColor blackColor];
    [self.playB setImage:[UIImage imageNamed:@"list_icon_music_mv@2x.png"] forState:UIControlStateNormal];
    [self.playB setImage:[UIImage imageNamed:@"download_icon_allstop@2x.png"] forState:UIControlStateSelected];
    [self.playB addTarget:self action:@selector(playAction) forControlEvents:UIControlEventTouchUpInside];
    [self.FootV addSubview:self.playB];

    
    self.stopB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.stopB.frame = CGRectMake(self.playB.frame.origin.x + self.playB.frame.size.width, self.playB.frame.origin.y, 40, 40);
    [self.stopB setImage:[UIImage imageNamed:@"list_icon_music_cd@2x.png"] forState:UIControlStateNormal];
    [self.stopB addTarget:self action:@selector(stopAction) forControlEvents:UIControlEventTouchUpInside];
    [self.FootV addSubview:self.stopB];
    
    self.nextB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextB.frame = CGRectMake(self.stopB.frame.origin.x + self.stopB.frame.size.width, self.stopB.frame.origin.y, 40, 40);
    [self.nextB setImage:[UIImage imageNamed:@"shortcut_play_icon@2x.png"] forState:UIControlStateNormal];
    [self.nextB addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    [self.FootV addSubview:self.nextB];
    
    self.timeS = [[UISlider alloc] initWithFrame:CGRectMake(0, self.FootV.frame.size.height - 4, self.view.frame.size.width, 4)];
    self.timeS.maximumValue = 193;
    [self.timeS addTarget:self action:@selector(timeAction) forControlEvents:UIControlEventValueChanged];
    self.timeS.thumbTintColor = [UIColor clearColor];
    self.timeS.minimumTrackTintColor = [UIColor orangeColor];
    [self.FootV addSubview:self.timeS];
    [_timeS release];
    
    self.songCover = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
    self.songCover.backgroundColor = [UIColor blackColor];
    [self.FootV addSubview:self.songCover];
    [_songCover release];
    
    self.song = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 200, 25)];
    self.song.backgroundColor = [UIColor redColor];
//    self.song.userInteractionEnabled = YES;
    [self.FootV addSubview:self.song];
    [_song release];
    
    self.singer = [[UILabel alloc] initWithFrame:CGRectMake(45, 25, 200, 20)];
    self.singer.backgroundColor = [UIColor blueColor];
//    self.singer.userInteractionEnabled = YES;
    [self.FootV addSubview:self.singer];
    [_singer release];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.singer addGestureRecognizer:tapGR];
    [self.song addGestureRecognizer:tapGR];
    [self.FootV addGestureRecognizer:tapGR];
    [tapGR release];
    
    
}

- (void)tapAction
{
    PlayViewController *playVC = [[PlayViewController alloc] init];
    [self presentViewController:playVC animated:YES completion:^{
        
        
    }];
}

- (void)timeAction
{
//    NSLog(@"oashd");
    [self.player seekToTime:self.timeS.value];
}

- (void)nextAction
{
    NSLog(@"tyiu");
}

- (void)stopAction
{
//    NSLog(@"qwer");
    [self.player stop];
    self.playB.selected = NO;
    [self.timer invalidate];

    self.timeS.value = 0;
}

- (void)playAction
{
    NSLog(@"asdf");
    if (self.player.state == STKAudioPlayerStatePlaying) {
        [self.player pause];
        self.playB.selected = NO;
    } else if (self.player.state == STKAudioPlayerStatePaused) {
        [self.player resume];
        self.playB.selected = YES;
    } else {
        [self.player play:@"http://m6.file.xiami.com/110/3110/15627/373965_2530192_h.mp3?auth_key=ce149eee428d8d9d85cba1e7f5c01206-1449770400-0-null"];
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(trackAction) userInfo:nil repeats:YES];
        
        self.playB.selected = YES;
    }
    
//    [PlayViewController sharePlayViewController].playB istou
//    NSLog(@"asdf");
    
}

- (void)trackAction
{
    self.timeS.value = self.player.progress;
}

- (void)segmentAction:(UISegmentedControl *)sender
{
    if (0 == sender.selectedSegmentIndex) {
        [self.view bringSubviewToFront:self.mineVC.view];
    }
    if (1 == sender.selectedSegmentIndex) {
        [self.view bringSubviewToFront:self.recommendVC.view];
    }
    [self.view bringSubviewToFront:sender];
}

- (void)personAction:(UIBarButtonItem *)sender
{
    
}

- (void)searchAction:(UIBarButtonItem *)sender
{
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
